/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import model.ContaPrePago;
import model.Operacao;

/**
 *
 * @author 
 */
@Stateless(mappedName="Operadora")
public class OperadoraBean implements OperadoraLocal, OperadoraRemote {
    
    @PersistenceContext(unitName="operadoracelularpu")
    private EntityManager em;
    
    private Operacao operacao;
    
    @Override
    public void inserirCredito(String numero,String cpf, double valor) {        
        ContaPrePago c = null;
        try {
            Query q = em.createQuery("select c from ContaPrePago c where c.numero = :n");
            q.setParameter("n", numero);
            c = (ContaPrePago)q.getSingleResult();            
        } catch (Exception e) {
            e.printStackTrace();
        }        
        operacao = new Operacao();
        operacao.setConta(c);
        operacao.setValorCreditado(valor);
        c.setSaldo(c.getSaldo()+valor);
        em.persist(operacao);
        em.merge(c);
    }

    @Override
    public void criarConta(ContaPrePago c) {
        em.persist(c);
    }

    @Override
    public ContaPrePago localizarNumero(String numero, String cpf) {
        ContaPrePago c = null;
        try {
            Query q = em.createQuery("select c from ContaPrePago c where c.numero = :n and c.cpf = :c");
            q.setParameter("n", numero);
            q.setParameter("c", cpf);
            c = (ContaPrePago)q.getSingleResult();            
        } catch (Exception e) {
            e.printStackTrace();
        } 
        return c;
    }

    
}
